package com.love.product.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.love.product.entity.Posts;
import com.love.product.entity.PostsLike;
import com.love.product.entity.base.Result;
import com.love.product.entity.base.ResultPage;
import com.love.product.entity.req.PostsPageReq;
import com.love.product.entity.req.PostsReq;
import com.love.product.entity.vo.PostsVO;
import com.love.product.entity.vo.UserInfoVO;
import com.love.product.enumerate.PostsType;
import com.love.product.enumerate.School;
import com.love.product.enumerate.YesOrNo;
import com.love.product.mapper.PostsMapper;
import com.love.product.service.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author hjf
 * @date 2022-10-19 10:26
 */
@Slf4j
@Service
public class PostsServiceImpl extends ServiceImpl<PostsMapper, Posts> implements PostsService {

    @Resource
    private FileUploadService fileUploadService;

    @Resource
    private UserInfoService userInfoService;

    @Resource
    private CollectService collectService;

    @Resource
    private FollowService followService;

    @Resource
    private PostsLikeService postsLikeService;

    /**
     * 发布帖子
     */
    @Override
    public Result<Posts> add(Long userId,PostsReq postsReq) {
        PostsType postsType = PostsType.valueOf(postsReq.getPostsType());
        School school = School.valueOf(postsReq.getSchool());
        if(postsType == null){
            return Result.failMsg("请选择帖子类型");
        }
        if(StringUtils.isBlank(postsReq.getTitle())){
            return Result.failMsg("标题不能为空");
        }
        if(StringUtils.isBlank(postsReq.getContent())){
            return Result.failMsg("内容不能为空");
        }
        if(school == null){
            return Result.failMsg("请选择校区");
        }
        List<String> imgPathList = new ArrayList<>();
        if(postsType.equals(PostsType.LEAVE)){//闲置帖
            if(postsReq.getPrice() == null || postsReq.getPrice().doubleValue() <= 0){
                return Result.failMsg("请输入价格");
            }
            if(postsReq.getFiles() == null || postsReq.getFiles().length == 0){
                return Result.failMsg("请至少上传一张图片");
            }
        }
        if(postsReq.getFiles() != null){
            if(postsReq.getFiles().length > 9){
                return Result.failMsg("最多可上传9张图片");
            }
            //上传图片
            for(MultipartFile multipartFile : postsReq.getFiles()){
                String imgPath = fileUploadService.uploadImage(multipartFile);
                imgPathList.add(imgPath);
            }
        }

        LocalDateTime now = LocalDateTime.now();
        Posts posts = new Posts();
        BeanUtil.copyProperties(postsReq,posts);
        posts.setId(IdWorker.getId());
        posts.setUserId(userId);
        posts.setCreateTime(now);
        posts.setUpdateTime(now);
        String msg = "发布成功";
        if(imgPathList.size() > 0){
            //图片需要审核
            msg = "发布成功，审核中~";
            posts.setStatus(YesOrNo.YES.getValue());
            posts.setCoverPath(imgPathList.get(0));
            posts.setImgPath(imgPathList.stream().map(String::valueOf).collect(Collectors.joining(",")));
        }
        boolean flag = save(posts);
        if(flag){
            return Result.OK(msg,posts);
        }
        return Result.failMsg("发布失败，请重试");
    }

    /**
     * 分页
     */
    @Override
    public ResultPage<PostsVO> getPage(Long userId,PostsPageReq postsPageReq) {
        if(userId == null && Objects.equals(postsPageReq.getSchool(),3)){
            return ResultPage.FAIL(403,"请登录");
        }
        LambdaQueryWrapper<Posts> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(postsPageReq.getPostsType()!=null,Posts::getPostsType, postsPageReq.getPostsType());
        queryWrapper.eq(postsPageReq.getSchool()!=null&&postsPageReq.getSchool()!=3,Posts::getSchool, postsPageReq.getSchool());
        queryWrapper.eq(Objects.equals(postsPageReq.getSchool(),3),Posts::getUserId, userId);
        queryWrapper.eq(Posts::getStatus, YesOrNo.NO.getValue());
        queryWrapper.orderByDesc(Posts::getCreateTime);
        Page<Posts> page = page(postsPageReq.build(), queryWrapper);
        List<PostsVO> list = new ArrayList<>();
        List<Long> userIds = new ArrayList<>();
        List<Long> postsIds = new ArrayList<>();
        if (page.getTotal() > 0) {
            list = page.getRecords().stream().map(posts -> {
                PostsVO postsVO = BeanUtil.copyProperties(posts, PostsVO.class);
                School school = School.valueOf(postsVO.getSchool());
                postsVO.setSchoolName(school!=null?school.getText():"");
                initImgPath(postsVO);
                userIds.add(postsVO.getUserId());
                postsIds.add(postsVO.getId());
                return postsVO;
            }).collect(Collectors.toList());
        }
        Map<Long, UserInfoVO> userInfoVOMap;
        Map<Long, PostsLike> postsLikeHashMap;
        if(Objects.equals(postsPageReq.getPostsType(),PostsType.SCHOOL.getValue()) && list.size() > 0){
            userInfoVOMap = userInfoService.listByIds(userIds);
            postsLikeHashMap = postsLikeService.listByUserId(userId,postsIds);
            Map<Long, UserInfoVO> finalUserInfoVOMap = userInfoVOMap;
            Map<Long, PostsLike> finalPostsLikeHashMap = postsLikeHashMap;
            list.forEach(item -> {
                UserInfoVO userInfoVO = finalUserInfoVOMap.get(item.getUserId());
                item.setUserInfo(userInfoVO);
                item.setLike(false);
                PostsLike postsLike = finalPostsLikeHashMap.get(item.getId());
                if(postsLike != null){
                    item.setLike(true);
                }
            });
        }
        return ResultPage.OK(page.getTotal(), page.getCurrent(), page.getSize(), list);
    }

    /**
     * 详情
     *
     */
    @Override
    public Result<PostsVO> getDetail(Long userId,Long id) {
        Posts posts = getById(id);
        if(posts == null || posts.getStatus().equals(YesOrNo.YES.getValue())){
            return Result.failMsg("帖子不存在或已下架");
        }
        PostsVO postsVO = BeanUtil.copyProperties(posts, PostsVO.class);
        School school = School.valueOf(postsVO.getSchool());
        postsVO.setSchoolName(school!=null?school.getText():"");
        UserInfoVO userInfoVO = userInfoService.getUserInfoById(posts.getUserId());
        postsVO.setUserInfo(userInfoVO);
        initImgPath(postsVO);
        postsVO.setCollect(false);
        postsVO.setFollow(false);
        if(userId != null && collectService.getDetail(userId,posts.getId()) != null){
            postsVO.setCollect(true);
        }
        if(userId != null && followService.getDetail(userId,posts.getUserId()) != null){
            postsVO.setFollow(true);
        }
        return Result.OK(postsVO);
    }

    /**
     * 更新浏览次数
     */
    @Override
    public Result<?> browse(Long id) {
        Posts posts = getById(id);
        if(posts != null){
            posts.setBrowseNum(posts.getBrowseNum() + 1);
            saveOrUpdate(posts);
        }
        return Result.OK();
    }

    /**
     * 拼接图片获取绝对路径
     */
    private void initImgPath(PostsVO postsVO){
        postsVO.setCoverPath(fileUploadService.getImgPath(postsVO.getCoverPath()));
        if(StringUtils.isNotEmpty(postsVO.getImgPath())){
            String[] arr = postsVO.getImgPath().split(",");
            List<String> list = Arrays.asList(arr);
            List<String> imgPathList = new ArrayList<>();
            list.forEach(item-> {
                imgPathList.add(fileUploadService.getImgPath(item));
            });
            postsVO.setImgPath(imgPathList.stream().map(String::valueOf).collect(Collectors.joining(",")));
        }
    }

    /**
     * 批量获取
     */
    @Override
    public Map<Long, PostsVO> listByIds(List<Long> postsIds){
        Map<Long, PostsVO> postsHashMap = new HashMap<>();
        LambdaQueryWrapper<Posts> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(Posts::getId,postsIds);
        List<Posts> postsList = list(queryWrapper);
        postsList.forEach(item -> {
            PostsVO postsVO = BeanUtil.copyProperties(item, PostsVO.class);
            initImgPath(postsVO);
            postsHashMap.put(postsVO.getId(), postsVO);
        });
        return postsHashMap;
    }
}
